// 用户管理路由
import { Router } from 'express';
import userController, { upload } from '@/controllers/system/user.controller';
import { authMiddleware } from '@/middlewares/auth.middleware';
import { permissionMiddleware } from '@/middlewares/permission.middleware';
import { dataScopeMiddleware } from '@/middlewares/dataScope.middleware';
import { createController } from '@/utils/router/route.controller';
import { 
  validateUserCreate, 
  validateUserUpdate, 
  validateResetPwd, 
  validateChangeStatus, 
  validateUpdateProfile, 
  validateUpdatePwd, 
  validateRemove,
  validateAvatarUpload
} from '@/validators/system/user.validator';

const router = createController(Router());

/**
 * 用户管理路由
 * 路径前缀：/system/user
 */

// 所有路由均需认证
router.use(authMiddleware);

// ================== 导入导出相关 ==================
// 导出用户
router.post('/export', permissionMiddleware('system:user:export'), userController.export);

// 导入用户数据
router.post('/importData', upload.single('file'), permissionMiddleware('system:user:import'), userController.importData);

// 下载导入模板
router.post('/importTemplate', userController.importTemplate);

// ================== 用户状态和密码相关 ==================
// 重置密码
router.put('/resetPwd', 
  permissionMiddleware('system:user:resetPwd'), 
  validateResetPwd,
  userController.resetPwd
);

// 修改用户状态
router.put('/changeStatus', 
  permissionMiddleware('system:user:edit'), 
  validateChangeStatus,
  userController.changeStatus
);

// ================== 授权角色相关 ==================
// 查询用户角色
router.get('/authRole/:userId', permissionMiddleware('system:user:query'), userController.authRole);

// 分配用户角色
router.put('/authRole', permissionMiddleware('system:user:edit'), userController.insertAuthRole);

// ================== 个人信息相关 ==================
// 获取个人信息
router.get('/profile', userController.profile);

// 修改个人信息
router.put('/profile', 
  validateUpdateProfile,
  userController.updateProfile
);

// 修改个人密码
router.put('/profile/updatePwd', 
  validateUpdatePwd,
  userController.updatePwd
);

// 上传头像
router.post('/profile/avatar', upload.single('avatarfile'), validateAvatarUpload, userController.uploadAvatar);

// ================== 部门树相关 ==================
// 获取部门树
router.get('/deptTree', userController.deptTree);

// ================== 用户列表与详情 ==================
// 用户列表
router.get('/list', dataScopeMiddleware(), permissionMiddleware('system:user:list'), userController.list);

// 用户详情
router.get('/:userId', permissionMiddleware('system:user:query'), userController.getInfo);

// 获取所有角色和岗位列表
router.get('/', userController.getRolesAndPosts);

// ================== 新增、修改、删除 ==================
// 新增用户
router.POST('', 
  permissionMiddleware('system:user:add'), 
  validateUserCreate,
  userController.add
);

// 修改用户
router.PUT('', 
  permissionMiddleware('system:user:edit'), 
  validateUserUpdate,
  userController.update
);

// 删除用户
router.DELETE('/:userIds', 
  permissionMiddleware('system:user:remove'), 
  validateRemove,
  userController.remove
);

export default router;